package com.jdy.hotel.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import androidx.collection.LruCache;

import java.util.HashMap;
import java.util.Map;

public class StorageManagerUtil {

    private SharedPreferences sharedPreferences;

    private static Context sContext;

    private static Map<String, SharedPreferences> sharedPreferencesUtilMap;

    private static Map<String, LruCache<String, Object>> lruCacheMap;

    private static int sMaxSize = 20;

    public static void init(Context context, int maxSize) {
        sContext = context;
        lruCacheMap = new HashMap<>();
        sMaxSize = maxSize;
        sharedPreferencesUtilMap = new HashMap<>();
    }

    public static void init(Context context) {
        SharedPreferencesUtil.init(context);
        lruCacheMap = new HashMap<>();
    }

    /**
     * 清除缓存
     */
    public static void clearCache() {
        for (LruCache<String, Object> objectLruCache : lruCacheMap.values()) {
            objectLruCache.evictAll();
        }
    }

    /**
     * 清除指定模块的缓存
     */
    public static void clearCache(String module) {
        if (lruCacheMap == null) return;
        LruCache<String, Object> cache = lruCacheMap.get(module);
        if (null == cache) return;
        cache.evictAll();
    }

    /**
     * 清除指定模块下指定key的缓存
     */
    public static void clearCache(String module, String key) {
        if (lruCacheMap == null) return;
        LruCache<String, Object> cache = lruCacheMap.get(module);
        if (null == cache) return;
        cache.remove(key);
    }

    /**
     * 清除指定模块的本地数据
     */
    public static void clearData(String module) {
//        SharedPreferencesUtil.getInstance(module).clear();
    }

    /**
     * 清除指定模块下指定key的本地数据
     */
    public static void clearData(String module, String key) {
//        SharedPreferencesUtil.getInstance(module).remove(key);
    }

    /**
     * 存储到缓存
     */

    public static Object putToCache(String module, String key, Object value) {
        if (null == value) return null;
        CollectionUtils.computeIfAbsent(lruCacheMap, module, v -> new LruCache<>(sMaxSize)).put(key, value);
//        SharedPreferencesUtil.getInstance(module).put(key, value);
        return value;
    }

    /**
     * 获取数据
     */
    public static <T> T get(String module, String key, T defaultValue) {
        if (TextUtils.isEmpty(module)) {
            return defaultValue;
        }

        if (TextUtils.isEmpty(key)) {
            return defaultValue;
        }

        Object v = getFromCache(module, key);
        if (null == v) {
//            if (null == defaultValue) {
////                defaultValue; defaultValue = "";
//            }
//            return putToCache(module, key, SharedPreferencesUtil.getInstance(module).get(key, defaultValue));
        }
        return (T) v;
    }


    private static Object getFromCache(String module, String key) {
        if (null == lruCacheMap) {
            return null;
        }
        LruCache<String, Object> cache = lruCacheMap.get(module);

        if (null == cache) return null;

        return cache.get(key);
    }
}
